package com.nbjtjc.safe.service.sys;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.primitives.Ints;
import com.nbjtjc.safe.mapper.sys.SysPermissionMapper;
import com.nbjtjc.safe.mapper.sys.SysRoleMapper;
import com.nbjtjc.safe.mapper.sys.SysRolePermissionMapper;
import com.nbjtjc.safe.model.sys.SysPermission;
import com.nbjtjc.safe.model.sys.SysRole;
import com.nbjtjc.safe.model.sys.SysRolePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    public SysRole detail(int roleId){
        SysRole role =null;
        if(roleId==0){
            return role;
        }
        role=sysRoleMapper.selectByPrimaryKey(roleId);
        List<SysRolePermission> rolePermissions=sysRolePermissionMapper.findByRoleId(roleId);
        List<Integer> leafIds=new ArrayList<>();
        int[] permissionIds;
        if(rolePermissions!=null&&rolePermissions.size()>0){
            permissionIds=new int[rolePermissions.size()];
            for(int i=0;i<rolePermissions.size();i++){
                permissionIds[i]=rolePermissions.get(i).getPermissionId();
            }
            List<SysPermission> sysPermissions=sysPermissionMapper.findSubPermissionCount(Ints.asList(permissionIds));
            if(CollectionUtil.isNotEmpty(sysPermissions)){
                HashSet<Integer> noLeafIds=new HashSet<>(sysPermissions
                        .stream().map(SysPermission::getParentId).collect(Collectors.toList()));
                for(int i=0;i<permissionIds.length;i++){
                    if(!noLeafIds.contains(permissionIds[i])){
                        leafIds.add(permissionIds[i]);
                    }
                }
            }else{
                leafIds.addAll(Ints.asList(permissionIds));
            }
        }else{
            permissionIds=new int[0];
        }
        role.setPermissionIds(permissionIds);
        role.setLeafIds(leafIds);
        return role;
    }

    @Transactional(rollbackFor = {Throwable.class})
    public SysRole add(SysRole role){
        if(role==null){
            return null;
        }
        sysRoleMapper.insert(role);
        if(role.getSysRolePermissions().size()>0){
            role.getSysRolePermissions().forEach(rolePermission-> {
                rolePermission.setRoleId(role.getId());
            });
            sysRolePermissionMapper.insertList(role.getSysRolePermissions());
        }
        return role;
    }

    @Transactional(rollbackFor = {Throwable.class})
    public SysRole update(SysRole role){
        if(role==null||role.getId()==null){
            return role;
        }
        sysRolePermissionMapper.deleteByRoleId(role.getId());
        sysRoleMapper.updateByPrimaryKeySelective(role);
        if(role.getSysRolePermissions().size()>0){
            role.getSysRolePermissions().forEach(rolePermission -> {
                rolePermission.setRoleId(role.getId());
            });
        }
        if(CollectionUtil.isNotEmpty(role.getSysRolePermissions())){
            sysRolePermissionMapper.insertList(role.getSysRolePermissions());
        }
        SysRole temp=detail(role.getId());
        return temp;
    }
}
